<?php


/**
 * 
 * 根据话题抓取微博数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","cron_get_mblog_by_topic_tag");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;
echo "<pre>";
//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/qq/inc.php");


class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$mblog_from_api = new mblog_from_api();
		
		//获取tag的总数
		$count = $db_mblog_cron_topic_tag->cron_get_mblog_topic_count($this->start_id);
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			
			$this->start_id = 0 ;
			$count = $db_mblog_cron_topic_tag->cron_get_mblog_topic_count($this->start_id);
		}
		
		
		
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
			
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		global $config;
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$db_cron_get_manage = 	new db_cron_get_manage();
		//分批次取tag名称
		$food_tag = $db_mblog_cron_topic_tag->cron_get_mblog_topic_list($this->start_id,$p,PCOUNT);
		
		foreach($food_tag as $value)
		{
			$topic_id = $value["topic_id"];
			$site_name= $config["site_name"][$topic_id];
			if(!$site_name)
			{
				continue;
			}
			
			$id = $value["id"];
			$cron_start_time  = $value["cron_start_time"];
			$black_word  = $value["black_word"];
			$tag_name  = $value["tag_name"];
			$desc  = $value["desc"];//别名
			if($id % TOTAL_PROCESS != NOW_PROCESS)//用总进程做控制 example: 3个进程  php xxx 1 只会执行除3余1的id
			{
				continue;
			}
			
			$show_type = $value["show_type"];
	
			
			
			if($desc)
			{
				$desc_array= explode("\r\n",$desc);
				foreach($desc_array as $tag_alias)
				{
					
					$get_key = "qqmblog_by_tag_".$site_name."_".$tag_alias;
					$get_res = $db_cron_get_manage->cron_get_item_by_key($get_key);
					$cron_start_time  = $get_res["get_num"];
					
					$this->get_list_by_topic($site_name,$tag_alias,$id,1,$cron_start_time,$show_type,$black_word,$topic_id,$get_key);
	
					$site_alias= $config["site_alias"][$topic_id];
					if($site_alias)
					{
						foreach($site_alias as $alias)
						{
							$get_key = "qqmblog_by_tag_".$alias."_".$tag_alias;
							$get_res = $db_cron_get_manage->cron_get_item_by_key($get_key);
							$cron_start_time  = $get_res["get_num"];
							
							$this->get_list_by_topic($alias,$tag_alias,$id,1,$cron_start_time,$show_type,$black_word,$topic_id,$get_key);
						}
					}
				
					
				}
				
			}
			
			
			
			
			if($tag_name)//处理抓取分页逻辑
			{
				$get_key = "qqmblog_by_tag_".$site_name."_".$tag_name;
				$get_res = $db_cron_get_manage->cron_get_item_by_key($get_key);
				$cron_start_time  = $get_res["get_num"];
				
				$this->get_list_by_topic($site_name,$tag_name,$id,1,$cron_start_time,$show_type,$black_word,$topic_id,$get_key);

				$site_alias= $config["site_alias"][$topic_id];
				if($site_alias)
				{
					foreach($site_alias as $alias)
					{
						$get_key = "qqmblog_by_tag_".$alias."_".$tag_name;
						$get_res = $db_cron_get_manage->cron_get_item_by_key($get_key);
						$cron_start_time  = $get_res["get_num"];
						
						$this->get_list_by_topic($alias,$tag_name,$id,1,$cron_start_time,$show_type,$black_word,$topic_id,$get_key);
					}
				}
				
			}
			
			
			
			
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		}
		
		if($done_job == false)
		{
			$this->ini_start_id();
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($site_name,$tag_name,$id,$p=1,$starttime="",$show_type="",$black_word="",$topic_id="",$get_key="")
	{
		global $config;
		
		$black_word_array = explode("\r\n",$black_word);
		$black_word_array[] = "收听";
					
					
		$mblog_get_mblog_from_qq = new mblog_get_mblog_from_qq();
		$pcount= 30;
		$max_time = "";
		//执行时间超过30分钟，退出
		if(time() - CRON_START_TIME > 900)
		{
			exit;
		}
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		$user_info = new user_info();
		$topic = $site_name ." ".$tag_name;
		
		$db_mblog_pic_content = new db_mblog_pic_content();
		if($topic)
		{
			if($starttime)
			{
				$end_time=time();
			}
			$mblog_list = $mblog_get_mblog_from_qq->get_mblog_list_by_search($topic,$pcount,$p,"4","1","",$starttime,$end_time);
			
			echo "{$topic}:count:{$mblog_list["data"]["totalnum"]}|get_count".count($mblog_list["data"]["info"]).":p:{$p}<br>\n";	
			
			/*$user_info = $mblog_get_mblog_from_qq->get_current_user_info();
			print_r($user_info);*/
			
			if($mblog_list["data"]["info"])
			{
				foreach($mblog_list["data"]["info"] as $value1)
				{
						
					
					
					echo "time_compare:".$value1["timestamp"]." == {$starttime}<br>\n";
					
					//如果时间跟最大时间相同，跳过
					/*if(strtotime($value1["created_at"]) == $starttime)
					{
						continue;
					}*/
				
					$check_source = $value1["from"];
					var_dump($check_source);
					$check_res = true;
					/*foreach($config["check_mblog_cron_source"] as $value)
					{
						if(stripos($check_source,$value) !== false)
						{
							$check_res = true;
							break;
						}
					}*/
					
					
					//黑名单处理开始
					if($black_word_array)
					{
						foreach($black_word_array as $word) 
						{
							if($word)
							{
								echo "checj_word:{$word}";
								var_dump(strpos($value1["origtext"],$word));
								if(strpos($value1["origtext"],$word) !== false)
								{
									echo "check_match_wrod: $word";
									var_dump($value1["origtext"]);
									$check_res = false;
									break;
								}
							}
							
						}
					}
					echo "check_";
					var_dump($check_res);
					
						
					//黑名单处理结束
					
				
					if($check_res == true )
					{
						
						$u_time = $value1["timestamp"];
						if($max_time < $u_time) $max_time = $u_time;
						$create_time = date("Y-m-d H:i:s",$u_time);
						
						$pic_url = $value1["image"][0]."/460";
						echo "image:{$pic_url}\n";
						
						$res = get_image::get_image_size_by_url($pic_url);
						
						$srcW = $res["width"]; 
						$srcH = $res["height"]; 
						if($srcW < 220) //实际大小已经小于220的就不要
						{
							echo "width < 220 : {$srcW} \n";
							continue;
						}
						
						$pic_width_pubu = PUBU_WIDTH;
						$pic_height_pubu = intval($srcH/($srcW/$pic_width_pubu));
			
						/*if($pic_height_pubu < 110 ) //经过计算后大于小于100和大于800的都不要，不然比例太恶心
						{
							echo "height < 110 : {$pic_height_pubu} \n";
							continue;
						}*/
						
						$value1["text"] = $value1["origtext"];
						$md5_content = md5($value1["text"]);
						$date =  date("Y-m-d",$u_time);
						$content = $value1["text"];
						$source =  $value1["from"];
						$nickname = $value1["nick"];
						$qq_user_info = $user_info->get_user_info_by_openid($value1["openid"]);
						$uid = $qq_user_info["uid"];
						$mid = $value1["id"];
						$head_url = $value1["head"];
						$love_count = $value1["reposts_count"] + $value1["comments_count"] ;
						
						$from = "1";
						$db_mblog_pic_content->cron_insert_mblog_pic_content($content,$topic_id,$tag_name,$pic_url,$pic_width_pubu,$pic_height_pubu,$source,$show_type,$from,
	$uid,$nickname,$date,$mid,$create_time,$count_love,$head_url);
						
					}
				}
		
			}
				
		}
		if($sql_status_tag_array)
		{
			$sql  = $sql_status_tag. join(",",$sql_status_tag_array);
			$db_base->query($sql);
			unset($sql_status_tag_array);
		}
		
		if($p == 1)//当为第一页时，检查是否有多页需要抓取
		{	//最大页数为20，接口限制
			$total_page = ceil($mblog_list["data"]["totalnum"]/$pcount);
			$total_page = $total_page > 50 ? 50 : $total_page;
			echo "topic_page_count:{$total_page}<br>\n";
			var_dump("$max_time && $max_time > $starttime");
			if($max_time && $max_time > $starttime)
			{
				/*$sql ="update mblog_cron_topic_tag  set cron_start_time = {$max_time} where id ={$id}";
				$db_base->query($sql);*/
				$db_cron_get_manage = new db_cron_get_manage();
				$get_res = $db_cron_get_manage->cron_replace_into_data($get_key,$max_time);
			}
			
			
			for($i = 2 ; $i <= $total_page;$i++)
			{
				$this->get_list_by_topic($site_name,$tag_name,$id,$i,$starttime,$show_type,$black_word,$topic_id,$get_key);
			}
			
			
		}
			
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
